using System;
using System.Collections.Generic;
using System.Reflection;
using UnityEngine;

namespace AdvancedInspector
{
	[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field)]
	public class CollectionAttribute : Attribute, IListAttribute, IRuntimeAttribute, IRuntimeAttribute<object>
	{
		public delegate object CollectionDelegate();

		private int size = -1;

		private bool sortable = true;

		private CollectionDisplay display;

		private int maxDisplayedItems = 25;

		private int maxItemsPerRow = 6;

		private Type enumType;

		private string methodName = string.Empty;

		private List<Delegate> delegates = new List<Delegate>();

		public int Size
		{
			get
			{
				return size;
			}
			set
			{
				size = value;
			}
		}

		public bool Sortable
		{
			get
			{
				return sortable;
			}
			set
			{
				sortable = value;
			}
		}

		public CollectionDisplay Display
		{
			get
			{
				return display;
			}
			set
			{
				display = value;
			}
		}

		public int MaxDisplayedItems
		{
			get
			{
				return maxDisplayedItems;
			}
			set
			{
				maxDisplayedItems = value;
			}
		}

		public int MaxItemsPerRow
		{
			get
			{
				return maxItemsPerRow;
			}
			set
			{
				maxItemsPerRow = value;
			}
		}

		public Type EnumType
		{
			get
			{
				return enumType;
			}
			set
			{
				if (!value.IsEnum)
				{
					return;
				}
				int num = 0;
				foreach (object value2 in Enum.GetValues(value))
				{
					if ((int)value2 != num)
					{
						return;
					}
					num++;
				}
				enumType = value;
			}
		}

		public string MethodName
		{
			get
			{
				return methodName;
			}
		}

		public Type Template
		{
			get
			{
				return typeof(CollectionDelegate);
			}
		}

		public List<Delegate> Delegates
		{
			get
			{
				return delegates;
			}
			set
			{
				delegates = value;
			}
		}

		public CollectionAttribute()
		{
		}

		public CollectionAttribute(int size)
			: this(size, true, string.Empty)
		{
		}

		public CollectionAttribute(Type enumType)
			: this(enumType, true, string.Empty)
		{
		}

		public CollectionAttribute(bool sortable)
			: this(-1, sortable, string.Empty)
		{
		}

		public CollectionAttribute(string methodName)
			: this(-1, true, methodName)
		{
		}

		public CollectionAttribute(int size, bool sortable)
			: this(size, sortable, string.Empty)
		{
		}

		public CollectionAttribute(int size, string methodName)
			: this(size, true, methodName)
		{
		}

		public CollectionAttribute(Type enumType, bool sortable)
			: this(enumType, sortable, string.Empty)
		{
		}

		public CollectionAttribute(Type enumType, string methodName)
			: this(enumType, true, methodName)
		{
		}

		public CollectionAttribute(bool sortable, string methodName)
			: this(-1, sortable, methodName)
		{
		}

		public CollectionAttribute(int size, bool sortable, string methodName)
		{
			this.size = size;
			this.sortable = sortable;
			this.methodName = methodName;
		}

		public CollectionAttribute(Type enumType, bool sortable, string methodName)
		{
			EnumType = enumType;
			this.sortable = sortable;
			this.methodName = methodName;
		}

		public CollectionAttribute(Delegate method)
		{
			delegates.Add(method);
		}

		public object Invoke(int index)
		{
			if (delegates.Count == 0 || index >= delegates.Count)
			{
				return null;
			}
			try
			{
				return delegates[index].DynamicInvoke();
			}
			catch (Exception innerException)
			{
				if (innerException is TargetInvocationException)
				{
					innerException = ((TargetInvocationException)innerException).InnerException;
				}
				Debug.LogError(string.Format("Invoking a method from a collection constructor failed. The exception was \"{0}\".", innerException.Message));
				return null;
			}
		}
	}
}
